'use strict';

const xtpl = require('xtpl');
const path = require('path');
const md5 = require('md5');
const captchapng = require('captchapng');
const db = require(path.join(__dirname,'../config/config'));
const CommonHelper = require(path.join(__dirname,"../helpers/commonHelpers"));


module.exports.classList = function(req,res){
    //判断是否登录
    CommonHelper.isLogin(req,res);
    //当前页码值
    const page = parseInt(req.query.page || 1);

    //每页显示多少条
    const limit = 6;

    //偏移量
    const start = (page-1)*limit;

    let where = 1;

    db.table('class').where(where).count('*').then(function(count){

        //总页数
        const totalPage = count % limit == 0 ? count/limit : parseInt(count/limit)+1;

        const totalPageArray = [];

        //分页
        for(var i=1;i<=totalPage;i++)
        {
            totalPageArray.push(i);
        }

        //查询数据
        db.table('class').alias('class').join({
            table: 'school',
            join: 'left',
            as: 'type',
            on: ['school_id', 'school_id']
        }).where(where).order('class.class_id DESC').limit(start,limit).select().then(function(class_list){

            let data = {
                array:class_list,
                count:count,
                totalPageArray:totalPageArray,
                page:page,
                totalPage:totalPage,
            };
            CommonHelper.renderFile(req,res,"classList.html",data);
        });
    });
};

module.exports.classAdd = function(req,res){
    //判断是否登录
    CommonHelper.isLogin(req,res);

    db.table('school').select().then(function(school){
        db.table('class').select().then(function(class_list){
            let Data = {
                "school_list":school,
                "class_list":class_list
            };

            CommonHelper.renderFile(req,res,"classAdd.html",Data);
        });
    });        
};

module.exports.classAddForm = function(req,res)
{
    //判断是否登录
    CommonHelper.isLogin(req,res);

    //先获取表单的数据

    //先检测一下 该用户是否存在
    let class_name = req.body.class_name;
    db.table('class').where(`class_name = '${class_name}'`).find().then(function(data){
        if(JSON.stringify(data) != "{}")
        {
            res.setHeader("Content-Type","text/html;charset=utf-8");
            res.end("<script>alert('该班级已存在');location.href='/class/classAdd';</script>");
        }else{
            let data = {
                "class_name":req.body.class_name,
                "school_id":req.body.school_id
            };

            db.table('class').add(data).then(function(insertId){
                if(insertId)
                {
                    //重定向
                    res.redirect(`/class/classList`);
                    return false;
                }else{
                    //重定向
                    res.redirect(`/class/classAdd`);
                    return false;
                }
            });
        }
    });
};

module.exports.classEdit = function(req,res)
{
    //判断是否登录
    CommonHelper.isLogin(req,res);

    let class_id = req.query.class_id;

    if(!class_id)
    {
        //重定向
        res.redirect(`/class/classList`);
        return false;
    }

    db.table('class').where(`class_id = ${class_id}`).find().then(function(class_list){
        db.table('school').select().then(function(school_list){
            let Data = {
                "class":class_list,
                "school_list":school_list
            };

            CommonHelper.renderFile(req,res,"classEdit.html",Data);
        });
    });
};

module.exports.classEditForm = function(req,res)
{
    //判断是否登录
    CommonHelper.isLogin(req,res);

    let class_id = req.query.class_id;

    if(!class_id)
    {
        //重定向
        res.redirect(`/class/classList`);
        return false;
    }

    db.table('class').where(`class_id = ${class_id}`).find().then(function(class_list){

        let data = {
            "class_name":req.body.class_name,
            "school_id":req.body.school_id
        };

        db.table('class').where(`class_id = ${class_id}`).update(data).then(function(affectId){
            if(affectId)
            {
                //重定向
                res.redirect(`/class/classList`);
                return false;
            }else{
                //重定向
                res.redirect(`/class/classEdit?class_id=${class_id}`);
                return false;
            }
        });
    });
};

module.exports.classDelete = function(req,res){

    let class_id = req.query.class_id;

    if(!class_id)
    {
        //重定向
        res.redirect(`/class/classList`);
        return false;
    }
    
    db.table('class').where(`class_id = ${class_id}`).delete().then(function(affectId){
        if(affectId){
            console.log('删除成功');
            //重定向
            res.redirect(`/class/classList`);
            return false;
        }else{
            console.log('删除失败');
            //重定向
            res.redirect(`/class/classList`);
            return false;
        }
    });
};